package com.ars.order;

import com.ars.db.DBTools;
import com.ars.main.StageCollection;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.ResourceBundle;

public class OneResultController implements Initializable {
    @FXML
    public Button economyOrderButton;
    @FXML
    public Button businessOrderButton;
    @FXML
    public Button headOrderButton;
    @FXML
    private Label lbFlightName;
    @FXML
    private Label lbStartTime;
    @FXML
    private Label lbEndTime;
    @FXML
    private Label lbAddressFrom;
    @FXML
    private Label lbAddressTo;
    @FXML
    private Label lbHour;
    @FXML
    private Label lbEconomyPrice;
    @FXML
    private Label lbBusinessPrice;
    @FXML
    private Label lbHeadPrice;
    @FXML
    private Label lbEconomySeats;
    @FXML
    private Label lbBusinessSeats;
    @FXML
    private Label lbHeadSeats;
    @FXML
    private Label lbMin;

    private int fid;

    @FXML
    public void economyOrderAction() {
        openOrderStage("经济舱");
    }

    @FXML
    public void businessOrderAction() {
        openOrderStage("商务舱");
    }

    @FXML
    public void headOrderAction() {
        openOrderStage("头等舱");
    }

    private void openOrderStage(String seatType) {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("order.fxml"));
        Stage stage = new Stage();
        loader.setResources(new ResourceBundle() {
            @Override
            protected Object handleGetObject(String key) {
                if (key.equals("seatType")) {
                    return seatType;
                } else if (key.equals("flightName")) {
                    return lbFlightName.getText();
                } else if (key.equals("price")) {
                    return lbHeadPrice.getText();
                } else if (key.equals("flightID")) {
                    return fid + "";
                } else if (key.equals("seatsRemain")) {
                    if (seatType.equals("头等舱")) {
                        return lbHeadSeats.getText();
                    } else if (seatType.equals("商务舱")) {
                        return lbBusinessSeats.getText();
                    } else {
                        return lbEconomySeats.getText();
                    }
                }
                return null;
            }

            @Override
            public Enumeration<String> getKeys() {
                return null;
            }
        });
        try {
            stage.setScene(new Scene(loader.load()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        stage.setTitle("订单确认");
        stage.show();
        StageCollection.getInstance().put("orderStage", stage);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        fid = (int) resources.getObject("fid");
        System.out.println(fid);
        String sql = "SELECT * FROM flight,schedule WHERE flight.sid = schedule.sid and fid = " + fid;
        ResultSet rs = new DBTools().query(sql);
        try {
            if (rs.next()) {
                lbFlightName.setText(rs.getString("name"));
                lbStartTime.setText(rs.getInt("hour_from") + ":" + rs.getInt("min_from"));
                lbEndTime.setText(rs.getInt("hour_to") + ":" + rs.getInt("min_to"));
                lbAddressFrom.setText(rs.getString("address_from"));
                lbAddressTo.setText(rs.getString("address_to"));
                int intervalMin = getIntervalMin(rs.getInt("hour_from"), rs.getInt("min_from"), rs.getInt("hour_to"), rs.getInt("min_to"));
                lbHour.setText(intervalMin / 60 + "");
                lbMin.setText(intervalMin % 60 + "");
                lbEconomyPrice.setText(rs.getInt("price") * 0.5 + "");
                lbBusinessPrice.setText(rs.getInt("price") * 0.7 + "");
                lbHeadPrice.setText(rs.getInt("price") + "");
                lbEconomySeats.setText(rs.getString("economy_seats_remain"));
                if (lbEconomySeats.getText().equals("0")) {
                    economyOrderButton.setDisable(true);
                }
                lbBusinessSeats.setText(rs.getString("business_seats_remain"));
                if (lbBusinessSeats.getText().equals("0")) {
                    businessOrderButton.setDisable(true);
                }
                lbHeadSeats.setText(rs.getString("first_seats_remain"));
                if (lbHeadSeats.getText().equals("0")) {
                    headOrderButton.setDisable(true);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private int getIntervalMin(int hourFrom, int minFrom, int hourTo, int minTo) {
        SimpleDateFormat format = new SimpleDateFormat("HH-mm");
        Date dateFrom = null;
        Date dateTo = null;
        try {
            dateFrom = format.parse(hourFrom + "-" + minFrom);
            dateTo = format.parse(hourTo + "-" + minTo);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int intervalMin = (int) ((dateTo.getTime() - dateFrom.getTime()) / 60000);
        return intervalMin < 0 ? intervalMin + 24 * 60 : intervalMin;
    }
}
